@inherits BaseEditContextSection

@if (Section != null && Section.IsVisible(EditContext.Entity, EditContext.EntityState))
{
    var renderFragment = Section.ToRenderFragment();

    if (renderFragment != null)
    {
        @renderFragment
    }
    else
    {
        var index = 0;

        @if (Section.Elements != null)
        {
            <tr draggable=@(UI.Reorderable && EditContext.Entity.Id != null ? "true" : "false")
                dropzone="true"
                @ondragstart="async (args) => DragInteraction.DragStart(args, EditContext.Entity.Id!)"
                @ondragenter="async (args) => DragInteraction.DragEnter(args, EditContext.Entity.Id!)"
                @ondragend="DragInteraction.DragEnd"
                class="@(DragInteraction.IsDraggedOver(EditContext.Entity.Id) ? "dropover" : "")
                    @(DragInteraction.IsDragged(EditContext.Entity.Id) ? "dragged" : "")
                    @(EditContext.IsModified() || EditContext.IsReordered() ? "modified" : "")">
                @{
                    foreach (var element in Section.Elements)
                    {
                        if (element is FieldUI field)
                        {
                            var name = (field as ExpressionFieldUI)?.Expression.PropertyName ?? (field as PropertyFieldUI)?.Property.PropertyName;
                            if (name == null)
                            {
                                continue;
                            }

                            if (!CollapseEmptyCells)
                            {
                                var position = UI.UniqueFields?.FindIndex(x => x.Name == field.Name) ?? UI.MaxUniqueFieldsInSingleEntity;

                                // fill in the gaps (when dealing with multiple entity variants)
                                if (position > index)
                                {
                                    <td colspan=@(position - index)>&nbsp;</td>

                                    index = position;
                                }
                            }

                            index++;

                            <td>
                                @if (field.IsVisible(EditContext.Entity, EditContext.EntityState))
                                {
                                    @field.ToRenderFragment(EditContext)
                                }
                            </td>
                        }
                    }
                    // fill in the gaps (when dealing with multiple entity variants)
                    if (!CollapseEmptyCells && UI.UniqueFields?.Count > index)
                    {
                        <td colspan=@(UI.UniqueFields.Count - index)>&nbsp;</td>
                    }
                    else if (UI.MaxUniqueFieldsInSingleEntity > index)
                    {
                        <td colspan=@(UI.MaxUniqueFieldsInSingleEntity - index)>&nbsp;</td>
                    }
                    if (UI.SectionsHaveButtons)
                    {
                        <ButtonCell Buttons="Section.Buttons" OnButtonClick="OnButtonClick" />
                    }
                }
            </tr>
        }
    }
}

@code {
    private bool CollapseEmptyCells => UI.EmptyVariantColumnVisibility == EmptyVariantColumnVisibility.Collapse;

    [Parameter] public ListUI UI { get; set; } = default!;
    [Parameter] public EventCallback<ButtonClickEventArgs> OnButtonClick { get; set; }
    [Parameter] public IDragInteraction DragInteraction { get; set; } = default!;

    protected override void OnInitialized()
    {
        base.OnInitialized();

        DragInteraction.DragStateChange += OnDragStateChange;
    }

    ~RowSection()
    {
        DragInteraction.DragStateChange -= OnDragStateChange;
    }

    private void OnDragStateChange(object? sender, EventArgs args)
    {
        StateHasChanged();
    }
}